<template>
	<view class="file-detail-container">
		<view>{{ fileName }}</view>
		<view style="margin-top: 32rpx;display: flex; justify-content: center;">
			<van-button
				@click="toPreview"
				type="primary"
			>
				查看
			</van-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				filePath: '',
				fileName: '',
				fileObjStr: ''
			};
		},
		onLoad (e) {
			this.fileObjStr = e.fileObj
			const fileObj = JSON.parse(e.fileObj)
			this.fileName = fileObj.fileName
			this.filePath = fileObj.filePath
		},
		methods: {
			toPreview () {
				uni.showToast({
					title: '预览'
				})
				uni.downloadFile({
					url: this.filePath,
					success: (res) => {
						uni.showToast({
							title: '文件下载成功'
						})
						var filePath = res.tempFilePath
						uni.openDocument({
						  filePath: filePath,
						  showMenu: true,
						  success: (res) => {
							console.log('打开文档成功')
						  }
						})
					},
					fail: () => {
						uni.showToast({
							icon: 'none',
							title: '文件下载失败'
						})
					}
				})
			},
			toDownload () {
				uni.downloadFile({
					url: this.filePath,
					success: (res) => {
						uni.showToast({
							title: '文件下载成功'
						})
					},
					fail: () => {
						uni.showToast({
							icon: 'none',
							title: '文件下载失败'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.file-detail-container {
	width: 100vw;
	height: 100vh;
	flex-direction: column;
	display: flex;
	justify-content: center;
	align-items: center;
}
</style>
